package com.mol.market.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

import com.mol.market.common.util.Utils;

/**
 *
 * 增加了滑动回调函数的ScrollView。
 *
 * Author: Andrew.Wang
 * Date  : 12-12-12
 */
public class NestedScrollView extends ScrollView {

    private boolean mScrollable = true;

    private OnScrollChangedListener mCallback;

    /**
     * Section View(float on the top when scroll over)
     */
    private View mSectionView;

    /**
     * Inner view
     */
    public View innerView;

    /**
     * 距离顶部的高度
     */
    private int mTop;

    public NestedScrollView(Context context) {
        super(context);
    }

    public NestedScrollView(Context context, AttributeSet attrs) {
         super(context, attrs);
    }

    public NestedScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 当前ScrollView是否可以滑动
     */
    public boolean isScrollable() {
        return mScrollable;
    }

    /**
     * 设置当前ScrollView是否能够滑动
     */
    public void setScrollable(boolean isScrollable) {
        mScrollable = isScrollable;
    }

//    private NestedScrollView mSubScrollView;
//
//    public void setSubScrollView(NestedScrollView scrollView) {
//        mSubScrollView = scrollView;
//    }

//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        if (mScrollable) {
//            return false;
//        }
//        return true;
//    }
//
//    //    @Override
//    public boolean onTouchEvent(MotionEvent ev) {
//
//        Utils.E("onTouchEvent >>>> " + getId() + " scrollable > " + mScrollable);
//        if (mScrollable) {
//
//            if (innerView != null) {
//                innerView.onTouchEvent(ev);
//            }
//            return true;
//        } else {
//            return super.onTouchEvent(ev);
//        }
//
////        switch (ev.getAction()) {
////            case MotionEvent.ACTION_DOWN:
////            case MotionEvent.ACTION_MOVE:
////                // if we can scroll pass the event to the superclass
////                if (mScrollable) {
////                    return super.onTouchEvent(ev);
////                }
////                Utils.E("not scrollable>>>>>>>>>");
////                // only continue to handle the touch event if scrolling enabled
////                return mScrollable; // mScrollable is always false at this point
////            default:
////                return super.onTouchEvent(ev);
////        }
//    }

    public void setOnScrollChangedListener(OnScrollChangedListener listener) {
        mCallback = listener;
    }

    @Override
    protected void onScrollChanged(int h, int v, int oldH, int oldV) {
        super.onScrollChanged(h, v, oldH, oldV);

//        if (mScrollable && innerView != null) {
//            innerView.scrollBy((h - oldH), (v - oldV));
//        }

        if (mCallback != null) {

//            Utils.E("onScrollChanged>>>>>>>>" + getId() + " height > " + v);
            mCallback.onScrollChanged(getScrollX(), getScrollY());

            if (v >= mTop) {

//                mCallback.onScrollChanged(h, v, oldH, oldV);
//                mScrollable = false;
//                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mSectionView.getLayoutParams();
//                params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//                mSectionView.setLayoutParams(params);
            } else {
//                mScrollable = true;
//                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mSectionView.getLayoutParams();
//                params.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
//                mSectionView.setLayoutParams(params);
            }
        }
    }

    public void setSectionViewResource(int res) {

        mSectionView = findViewById(res);

        int[] location = new int[2];
        int[] location2 = new int[2];
        mSectionView.getLocationOnScreen(location);
        mSectionView.getLocationInWindow(location2);

        for (int i = 0; i<location.length;i++) {
            Utils.E("*************" + location[i]);
        }
        for (int i = 0; i<location2.length;i++) {
            Utils.E("*************" + location2[i]);
        }
        mTop = location[1];
//        mTop = mSectionView.getTop();
//        Utils.E("mSectionView >>> top " + mTop);
//        View v = findViewById(R.id.info_action_2);
//        Utils.E("ActionView >>> top " + v.getTop());

    }
}
